#include "config.h"
#include "event_app.h"
#include "irq_contorl.h"

IrqControlNode_t IrqNodeHead = NULL;

bool AddIrqNewTable(IrqControlNode_t NewItem, pEventIrqHandle Event)
{
  if (NewItem == NULL)
    return false;

  NewItem->pNextItem = NULL;
  NewItem->pPreviousItem = IrqNodeHead;
  NewItem->pEventCallback = Event;

  IrqNodeHead = NewItem;

  return true;
}

bool DeleteSpecificTable(IrqControlNode_t IrqItem)
{
  if (IrqItem != NULL)
  {
    if (IrqItem->pPreviousItem != NULL)
    {
      IrqItem->pPreviousItem->pNextItem = IrqItem->pNextItem;
    }

    if (IrqItem->pNextItem != NULL)
    {
      IrqItem->pNextItem->pPreviousItem = IrqItem->pPreviousItem;
    }

    memset(IrqItem, 0, sizeof(struct IrqControlNode));

    return true;
  }

  return false;
}

void TraversalIrqTable(void)
{
  IrqControlNode_t IrqControlItem = IrqNodeHead;

  while (IrqControlItem != NULL)
  {
    if (IrqControlItem->pEventCallback != NULL)
    {
      if (IrqControlItem->pEventCallback() == true)
        return;
    }

    IrqControlItem = IrqControlItem->pPreviousItem;
  }
}
